better logic, needs more testing still