more code cleanup and directory check