Move some where stuff into re-usable functions. Also, adjust some of the types