Performance fixes

Following the discussion in #262