Fix naming issues and optimise and refactor code