Fix hash function, add some unit tests