UPDATE: refactor main.js logic

This should fix crashing issues with newer versions of Firefox.