refactored, added comments, and removed dead code