Cleanup build.js and move it to lib

Works fine locally on Linux. Someone else should probably test too.