some refactoring plus bug tracking