refactoring done. could not test yet though