refactored, some fixes