Bug fixes and restructing