slighty refactored