minor fixes and code arrangement