code is a bit cleaned up