Refactor and optimise code, Fix naming issues