Final code with full implementation
Final working code