added assertions
fixed some bugs that appeared at runtime