Bug fixes

@mattmo Can you please review?