Use ints instead of bools; tidy up a bit