Cleaned up with better comments added