p37 and some minor refactoring