Cleaned up pseudocode