Full rework of the sources

Mostly adding error checks.