Reorganized part of generate file... but it needs a lot more work