fix format and do some refactorings