simplified the code and added comments