refactored to allow more customization and fix ip glitch