Fixed various bugs to manual iplementation.