added the largest part of the init function