Added license information. Some fixes and optimizations