commented and cleaned up the code