Improve build script

Fixed an issue where deleting and rebuilding the build directory would
sometimes fail.