Optimise code and fix naming issues