Subdirectory support

- added support for subdirectories
- cleaned up code