Many fixes and improved functions