Code refactored and notes updated