rewritten code to make it more object oriented