optimise code to a class