Rearranging functions so those that rely on previously defined functions are defined later
Adding some quickcheck tests