Bug fixes and improvements

@sunglim See comments in the code comparison.
I think this is why directories used to always fall through to the next `if` (where you had that comment).