some reorganization of code