Fixing some bugs with refactor