Implemented MAP algorithm; cleaned up code.