Bug fixes and internal improvements