Added support for specifying a base directory, cleaned up code.