Refactored JS

Smaller, faster, stronger (i.e. accounts for midnight - 1am in a far
less clunky way), more maintainable.