Updated with latest source code (fixed some critical bugs, sorry!)