syntax fix
fix sort logic, more error handeling