Commented and cleaned up code