refactored, fixed a bug