Cleaned and optimized code, added local file support