Code cleanup and added more documentation