refactored and added comments for future ref