More fixes and refacturing

Fixes and refacturing.