added LGPL, re-arranged the code a bit