Complete Implementation with comments