Cleanups and added unittest