Bug fixes, merge with Dmitry