Some fixes and add main function