Fix naming issues and optimise code