Made code valid
Added exits, improved initialization routine