added more cross browser compatibility, cleaned up code