reordered code. minor refactoring