some refactoring and added comments;