Restructured logging in. contains bugs.