Optimization

Fixes #529 and makes the entire application significantly faster.