Ported the old BHMS.

Only the queries and the DB need to be changed, so I decided to port the
old instead of writing from scratch.