Build improvements and some bug fixes