cleaned code, slightly faster