Review the code carefully and improve where needed

@dartelov please refactor the code where needed. review it carefully and make sure it matches the latest core version standards.