Refactors sleep part a bit, fixes one bug