refactored, more idiomatic, fixed some FIXMEs