fix copy and paste bug, some misplaced if, etc