cleaned up code and added some documentations