Added code documentation and removed duplicate code