Narrowed logic, fixed some bugs