Major refactoring of the backend (improvement of performance)

 @pmalves: please review