some refactoring; segmenation fault fix